<?php

/**
 * 新达达开放平台发单接口DEMO
 * 详情：发单
 * 版本：1.0
 * 日期：2016-09-10
 * 说明：
 * 以下代码只是为了方便对接商户测试而提供的样例代码，对接商户可以根据自己的需求，按照技术文档编写，代码仅供参考。
 */
namespace distribution;

use app\lib\dadaapi\ToolsInco;
use app\common\model\Dadacity;
use app\common\model\Order;
use app\lib\dadaapi\DadaOpenapi;
use gaodeapi\gaoDeApi;
use think\Controller;


class DadaAddOrder extends Controller {

    /**
     * @param $param
     * @return bool|string
     * 参数序列化
     */
    public function paramSerialize($param){
        $paramStr = "?";
        foreach ($param as $key=>$item){
            $paramStr = $paramStr.$key."=".$item."&";
        }

        return $paramStr;
    }

    /**
     * 高德地图
     * 获取用户地址详细信息,包括经纬度
     */
    public function getAddres($data){

        $tools = new ToolsInco();
        $config = $tools->getGaodeConfig();
        $data['key']=$config['key'];
        $data['output']="json";

        $paramStr = "";
        foreach ($data as $key=>$item){
            $paramStr = $paramStr.$key."=".$item."&";
        }
        $url = "http://restapi.amap.com/v3/geocode/geo";

        $gaodeApi = new gaoDeApi();
        $res = $gaodeApi->getHttpRequestWithPost($url,$paramStr);

        return $res;
    }

    /**
     * @param $param
     * @return bool|string
     * 城市字符串截取
     */
    public function citySubstr($param){
        $cityName =  substr($param['cityName'],0,strpos($param['cityName'],"市"));
        return $cityName;
    }

    public function getCityCode($dataParam){
        $dataCityModel = new Dadacity();
        //**********************城市字符串截取****************************
        $cityName = $this->citySubstr($dataParam);
        //**********************获取城市编码列表****************************
        $cityName = $dataCityModel->getCity($cityName);
        return $cityName;
    }

    /**
     * @param $dataParam
     * 发送订单
     */
    public function addOrder($dataParam){
        $ToolsInco = new ToolsInco();
        //**********************获取收货人地址经纬度*************************
        $param['address'] = "武汉市洪山区曙光星城B区一栋一单元2001室";
        $getAdders = $this->getAddres($param);
        $res = json_decode($getAdders);
        $geocodes = explode(',',$res->geocodes[0]->location);
        $longitude = $geocodes[0];      //收货人地址经度
        $latitude = $geocodes[1];       //收货人地址纬度
        //*********************配置项*************************
        $config = $ToolsInco->getDataConfig();
        $config['url'] = $config['url'].'api/order/addOrder';
        $obj = new \app\lib\dadaapi\DadaOpenapi($config);
        //**********************获取城市编码列表****************************
        $getCityCode = $this->getCityCode($dataParam);
        $dataCityModel = new Dadacity();
        //***********************发单接口************************
        //发单请求数据,只是样例数据，根据自己的需求进行更改。
        $data = array(
            'shop_no'=> $config['shop_no'],
            'origin_id'=> $dataParam['orderId'],//
            'city_code'=> $getCityCode->cityCode,
            'info'=> '测试订单',
            'cargo_price'=> $dataParam['orderId'],
            'is_prepay'=> $dataCityModel::is_prepay_0,    //静止垫付
            'expected_fetch_time'=> time(),
            'receiver_name'=> '测试',
            'receiver_address'=> $dataParam['address'],
            'receiver_phone'=> '15927316422',
            'receiver_tel'=> '15927316422',
            'receiver_lat'=> $latitude,
            'receiver_lng'=> $longitude,
            'callback'=>config('miniprogram.domain').'/api/dadaNotify/index'
        );

        //请求接口
        $reqStatus = $obj->makeRequest($data);
        if (!$reqStatus) {
            //接口请求正常，判断接口返回的结果，自定义业务操作
            if ($obj->getCode() == 0) {
                echo '返回成功';
            }else{
                //返回失败
                echo '返回失败';
            }
            echo sprintf('code:%s，msg:%s', $obj->getCode(), $obj->getMsg());
        }else{
            //请求异常或者失败
            echo '请求异常';
        }
    }

    /**
     * 获取城市列表
     * 罗俊阳
     */
    public function getDadaCity(){
        $ToolsInco = new ToolsInco();
        $config = $ToolsInco->getDataConfig();
        $config['url'] =  $config['url']."/api/cityCode/list";
        $obj = new \app\lib\dadaapi\DadaOpenapi($config);
        $data = $obj->makeRequest(array());
        $dadaStatus = $obj->getStatus();
        $Result = $obj->getResult();
        $res['status']=$dadaStatus;
        $res['msg']=$obj->getMsg();
        $res['code'] = $obj->getCode();
        $res['result']=$Result;
        if($dadaStatus == "success"){
          return $res;
        }else{
            return false;
        }
    }

    /**
     * 查询运费后发单接口
     */
    public function addAfterQuery($param){
        $tools = new ToolsInco();
        $config = $tools->getDataConfig();
        $config['url'] = $config['url'].'/api/order/addAfterQuery';
        $dadaOpenApi = new DadaOpenapi($config);
        $param['url'] = $config['url'];

        //请求接口
        $reqStatus = $dadaOpenApi->makeRequest($param);

        if (!$reqStatus) {
            //接口请求正常，判断接口返回的结果，自定义业务操作
            if ($dadaOpenApi->getCode() == 0) {
                //返回成功
                $returnData['result'] = $dadaOpenApi->getResult();
                $returnData['code'] =$dadaOpenApi->getCode();
                $returnData['msg'] =$dadaOpenApi->getMsg();
                $returnData['status'] =$dadaOpenApi->getStatus();
                return $returnData;

            }else{
                //返回失败
                $returnData['code'] =$dadaOpenApi->getCode();
                $returnData['msg'] =$dadaOpenApi->getMsg();
                $returnData['status'] =$dadaOpenApi->getStatus();
               return $returnData;
            }
        }else{
            //返回失败
            $returnData['code'] ="-100";
            $returnData['msg'] ="请求失败";
            $returnData['status'] ="fail";
            return $returnData;
        }
    }




    public function orderQuery($date){
        $tools = new ToolsInco();
        $dadaConfig = $tools->getDataConfig();
        $dadaConfig['url'] = $dadaConfig['url'].'/api/order/status/query';
        $dadaOpenApi = new DadaOpenapi($dadaConfig);
        $reqStatus = $dadaOpenApi->makeRequest($date);

        if (!$reqStatus) {
            //接口请求正常，判断接口返回的结果，自定义业务操作
            if ($dadaOpenApi->getCode() == 0) {
                //返回成功
                $returnData['result'] = $dadaOpenApi->getResult();
                $returnData['code'] =$dadaOpenApi->getCode();
                $returnData['msg'] =$dadaOpenApi->getMsg();
                $returnData['status'] =$dadaOpenApi->getStatus();
                return $returnData;

            }else{
                //返回失败
                $returnData['code'] =$dadaOpenApi->getCode();
                $returnData['msg'] =$dadaOpenApi->getMsg();
                $returnData['status'] =$dadaOpenApi->getStatus();
                return $returnData;
            }
        }else{
            //返回失败
            $returnData['code'] ="-100";
            $returnData['msg'] ="请求失败";
            $returnData['status'] ="fail";
            return $returnData;
        }
    }

    /**
     * @param $param
     * @return mixed
     * 达达订单取消
     */
    public function formalCancel($param){
            $tools = new ToolsInco();
            $config = $tools->getDataConfig();
            $config['url'] = $config['url'].'/api/order/formalCancel';
            $DadaOpenApi = new DadaOpenapi($config);
            $reqStatus = $DadaOpenApi->makeRequest($param);
        if (!$reqStatus) {
            //接口请求正常，判断接口返回的结果，自定义业务操作
            if ($DadaOpenApi->getCode() == 0) {
                //返回成功
                $returnData['result'] = $DadaOpenApi->getResult();
                $returnData['code'] =$DadaOpenApi->getCode();
                $returnData['msg'] =$DadaOpenApi->getMsg();
                $returnData['status'] =$DadaOpenApi->getStatus();
                return $returnData;
            }else{
                //返回失败
                $returnData['code'] =$DadaOpenApi->getCode();
                $returnData['msg'] =$DadaOpenApi->getMsg();
                $returnData['status'] =$DadaOpenApi->getStatus();
                return $returnData;
            }
        }else{
            //返回失败
            $returnData['code'] ="-100";
            $returnData['msg'] ="请求失败";
            $returnData['status'] ="fail";
            return $returnData;
        }
    }

    /**
     * @param $order
     */
    private function is_status($order){
        $orderModel = new \app\common\model\Order();
        if($order->status == $orderModel::STATUS_1){
            $data = array(
                "code"=>config('status.ERROR_STATUS'),
                "status"=>config('status.ERROR_STATUS'),
                "message"=>"该订单未付款，不能配送"
            );
        }else{
            $data = array(
                "code"=>config('status.SUCCESS_STATUS'),
            );
        }
        return $data;
    }

    /**
     * 预配送订单
     */
    public function queryDeliverFee($data){
        $tools = new ToolsInco();
        //*********************配置项*************************
        $config = $tools->getDataConfig();
        $config['url'] = $config['url'].'/api/order/queryDeliverFee';
        //请求接口
        $dadaAddOrder = new DadaOpenapi($config);
        $reqStatus = $dadaAddOrder->makeRequest($data);
        if (!$reqStatus) {
            //接口请求正常，判断接口返回的结果，自定义业务操作
            if ($dadaAddOrder->getCode() == 0) {
                //返回成功
                $returnData['result'] = $dadaAddOrder->getResult();
                $returnData['code'] =$dadaAddOrder->getCode();
                $returnData['msg'] =$dadaAddOrder->getMsg();
                $returnData['status'] =$dadaAddOrder->getStatus();
                return $returnData;
            }else{
                //返回失败
                $returnData['code'] =$dadaAddOrder->getCode();
                $returnData['msg'] =$dadaAddOrder->getMsg();
                $returnData['status'] =$dadaAddOrder->getStatus();
                return $returnData;
            }
        }else{
            //返回失败
            $returnData['code'] ="-100";
            $returnData['msg'] ="请求失败";
            $returnData['status'] ="fail";
            return $returnData;
        }
    }

    //达达运费充值
    public function recharge($data){
        $tools = new ToolsInco();
        $config = $tools->getDataConfig();
        $config['url'] = $config['url'].'/api/recharge';
        //请求接口
        $dadaAddOrder = new DadaOpenapi($config);
        $reqStatus = $dadaAddOrder->makeRequest($data);
        if (!$reqStatus) {
            //接口请求正常，判断接口返回的结果，自定义业务操作
            if ($dadaAddOrder->getCode() == 0) {
                //返回成功
                $returnData['result'] = $dadaAddOrder->getResult();
                $returnData['code'] =$dadaAddOrder->getCode();
                $returnData['msg'] =$dadaAddOrder->getMsg();
                $returnData['status'] =$dadaAddOrder->getStatus();
                return $returnData;
            }else{
                //返回失败
                $returnData['code'] =$dadaAddOrder->getCode();
                $returnData['msg'] =$dadaAddOrder->getMsg();
                $returnData['status'] =$dadaAddOrder->getStatus();
                return $returnData;
            }
        }else{
            //返回失败
            $returnData['code'] ="-100";
            $returnData['msg'] ="请求失败";
            $returnData['status'] ="fail";
            return $returnData;
        }
    }



}



